package com.lottery.im.module.chat.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.GroupNotice;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.constant.RxBusCode;
import com.lottery.im.module.chat.adapter.GroupNoticeAdapter;
import com.lottery.im.module.chat.presenter.GroupNoticeListPresenter;
import com.lottery.im.module.chat.view.GroupNoticeListView;
import com.lottery.im.module.tim.model.GroupInfo;
import com.lottery.im.util.rxbus.RxBus;
import com.lottery.im.util.rxbus.Subscribe;
import com.tencent.imsdk.TIMGroupMemberRoleType;
import java.util.ArrayList;
import java.util.List;

/**
 * 群公告列表
 */
public class GroupNoticeListFragment extends BaseSwipeFragment<GroupNoticeListPresenter> implements
    GroupNoticeListView {

  private final static String ID_KEY = "ID_KEY";

  public static GroupNoticeListFragment create(String id) {
    GroupNoticeListFragment fragment = new GroupNoticeListFragment();
    Bundle b = new Bundle();
    b.putString(ID_KEY, id);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.iv_edit) ImageButton mIvEdit;
  @BindView(R.id.list) RecyclerView mList;
  @BindView(R.id.tv_empty_tip) TextView mTvEmptyTip;

  private String mGroupId;

  private List<GroupNotice> mGroupNotices;
  private GroupNoticeAdapter mAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_group_notice_list;
  }

  @Override protected void initPresenter() {
    presenter = new GroupNoticeListPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    RxBus.get().register(this);
    mGroupId = getArguments().getString(ID_KEY);
    TIMGroupMemberRoleType role = GroupInfo.getInstance().getRole(mGroupId);
    if (role == TIMGroupMemberRoleType.Owner || role == TIMGroupMemberRoleType.Admin) {
      mIvEdit.setVisibility(View.VISIBLE);
    }

    mList.setLayoutManager(new LinearLayoutManager(getActivity()));

    mGroupNotices = new ArrayList<>();
    mAdapter = new GroupNoticeAdapter(mGroupNotices);
    mAdapter.setOnItemClickListener((adapter, view, position) -> startFragmentForResult(
        GroupNoticeDetailFragment.create(mGroupNotices.get(position),position), 888));
    mList.setOnClickListener((v) -> {
    });
    mList.setAdapter(mAdapter);
    presenter.groupNoticeGetAll(mGroupId);
  }

  @OnClick({ R.id.iv_back, R.id.iv_edit }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.iv_edit:
        // 新增
        startFragment(GroupNoticeEditFragment.create(mGroupId));
        break;
    }
  }

  @Override public void getSuccess(List<GroupNotice> t) {
    mGroupNotices.clear();
    if (null == t || t.size() == 0) {
      mTvEmptyTip.setVisibility(View.VISIBLE);
      mList.setVisibility(View.GONE);
    } else {
      mGroupNotices.addAll(t);
      mTvEmptyTip.setVisibility(View.GONE);
      mList.setVisibility(View.VISIBLE);
    }
    mAdapter.notifyDataSetChanged();
  }

  @Override protected void onFragmentResult(int requestCode, int resultCode, Intent data) {
    super.onFragmentResult(requestCode, resultCode, data);

    if (requestCode == 888 && resultCode == RESULT_OK && null != data){
      presenter.groupNoticeGetAll(mGroupId);
    }

  }

  @Subscribe(code = RxBusCode.NOTICE_CODE)
  public void editSuc(String title) {
    popBackStack();
  }

  @Override public void onDestroy() {
    super.onDestroy();
    RxBus.get().unRegister(this);
  }
}
